package com.hxwang.tanke;

import java.awt.*;

import static com.hxwang.tanke.TankFrame.GAME_HEIGHT;
import static com.hxwang.tanke.TankFrame.GAME_WIDTH;

/**
 * <H2> 炮弹类 </H2>
 *
 * @author hxwang
 * @data 2022/12/4
 */

public class Bullet {
    private int speed = 10;// 炮弹的速度
    private int x, y; // 坐标
    private Dir dir; // 方向
    private boolean living = true; // 是否需要消失
    private TankFrame tf;
    private Group group; // 进行敌我区分

    public Rectangle rectangle = new Rectangle(); // 子弹所在位置对应的框框

    public static final int BHEIGHTL = ResoourcesMgr.bulletL.getHeight();
    public static final int BWIDTHL = ResoourcesMgr.bulletL.getWidth();

    public static final int BHEIGHTR = ResoourcesMgr.bulletR.getHeight();
    public static final int BWIDTHR = ResoourcesMgr.bulletR.getWidth();

    public static final int BHEIGHTU = ResoourcesMgr.bulletU.getHeight();
    public static final int BWIDTHU = ResoourcesMgr.bulletU.getWidth();

    public static final int BHEIGHTD = ResoourcesMgr.bulletD.getHeight();
    public static final int BWIDTHD = ResoourcesMgr.bulletD.getWidth();

    public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        rectangle.x = this.x;
        rectangle.y = this.y;
    }


    public void paint(Graphics g) {
        // 如果子弹消失，将其从容器中删除，防止内存溢出
        if (!living) {
            tf.bullets.remove(this);
        }
        Color color = g.getColor();
        switch (dir) {
            case LEFT:
                g.drawImage(ResoourcesMgr.bulletL, x, y, null);
                rectangle.width = BWIDTHL;
                rectangle.height = BHEIGHTL;
                break;
            case RIGHT:
                g.drawImage(ResoourcesMgr.bulletR, x, y, null);
                rectangle.width = BWIDTHR;
                rectangle.height = BHEIGHTR;
                break;
            case UP:
                g.drawImage(ResoourcesMgr.bulletU, x, y, null);
                rectangle.width = BWIDTHU;
                rectangle.height = BHEIGHTU;
                break;
            case DOWN:
                g.drawImage(ResoourcesMgr.bulletD, x, y, null);
                rectangle.width = BWIDTHD;
                rectangle.height = BHEIGHTD;
                break;
        }
//        g.setColor(Color.RED);
//        g.fillOval(x, y, WIDTH, HEIGHT);// 正方形的内切圆
        g.setColor(color);

        move();

    }

    private void move() {
        switch (dir) {
            case LEFT:
                x -= speed;
                break;
            case UP:
                y -= speed;
                break;
            case RIGHT:
                x += speed;
                break;
            case DOWN:
                y += speed;
                break;
        }
        // update rectangle
        rectangle.x = this.x;
        rectangle.y = this.y;
        if (x < 0 || y < 0 || x > GAME_WIDTH || y > GAME_HEIGHT) living = false;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public int getSpeed() {
        return speed;
    }

    public int getX() {
        return x;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public int getY() {
        return y;
    }

    public Dir getDir() {
        return dir;
    }

    // 子弹和坦克碰撞检测
    public void collideWith(Tank tank) {
        if (tank.getGroup() == this.group) return;
        Rectangle rectangle1 = this.rectangle;
        Rectangle rectangle2 = tank.rectangle;
        if (rectangle1.intersects(rectangle2)) {
            tf.explodes.add(new Explode(tank.getX(), tank.getY(), tf));
            tank.die();
            this.die();
        }

    }

    private void die() {
        this.living = false;
    }
}
